# Thread posting bot
# by Cee Pee

import os
import random
import re
import forumapi

def find_phrases():
	"""Returns all phrases files."""
	
	phrases = []
	for file in os.listdir("data"):
		m = re.match("^(.*)\.phrases$", file)
		if m:
			phrases.append(m.group(1))
	
	return(phrases)

def settings(settings):
	settings.add_option("--phrases", type="choice", choices=find_phrases(), help="phrases file", metavar="FILE")
	settings.add_option("--tags", type="list", help="tags to add to the thread")
	settings.add_arg("forum", type="int", help="forum ID to post in")
	settings.add_arg("title", optional=True, default="Thread title", help="thread title")
	settings.add_arg("text", optional=True, default="Post content", help="text to post")

def main(me, options, args):
	phrases = []
	if options.phrases:
		phrases = open(forumapi.data_file(options.phrases, "phrases"), encoding="utf8").read().split("\n")
	
	if phrases:
		phrase = random.choice(phrases)
	else:
		phrase = args.text
	
	print(repr(me.make_thread(phrase, args.title, args.forum, tags=options.tags)))

if __name__ == "__main__":
	forumapi.run(main, options=settings, supported_types=["vbulletin", "vanilla"])
